﻿public class C正则
{

    public static bool IsMatch(string 正则表达式, string 字符串, RegexOptions 正规选项)
    {
        try
        {
            Regex testRegex = new Regex(正则表达式, 正规选项);
            if (testRegex.IsMatch(字符串))
                return true;
            else
                return false;
        }
        catch (ArgumentException ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("\nThere was an error in your regular expression:\r\n" + ex.Message);
            Console.ResetColor();
            return false;
        }
    }

    //using System.Text.RegularExpressions;
    //using System.Collections;
    public static ArrayList RegexMatches(string 正则表达式, string 字符串, RegexOptions 正则选项)
    {
        try
        {
            Regex matchesRegex = new Regex(正则表达式, 正则选项);
            MatchCollection matchesFound;
            matchesFound = matchesRegex.Matches(字符串);
            ArrayList rAL = new ArrayList();
            foreach (Match matchMade in matchesFound)
                rAL.Add(matchMade.Value);
            return rAL;
        }
        catch (ArgumentException ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("\nThere was an error in your regular expression:\r\n" + ex.Message);
            Console.ResetColor();
            ArrayList nAL = new ArrayList();
            return nAL;
        }
    }

    public static string RegexMatches(string 正则表达式, string 字符串, RegexOptions 正则选项, int 从零起返回第几项)
    {
        ArrayList rAL = new ArrayList();
        try
        {
            Regex matchesRegex = new Regex(正则表达式, 正则选项);

            MatchCollection matchesFound;
            matchesFound = matchesRegex.Matches(字符串);

            //ArrayList rAL = new ArrayList();

            foreach (Match matchMade in matchesFound)
                rAL.Add(matchMade.Value);
            return rAL[从零起返回第几项].ToString();
        }
        catch (ArgumentException ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("\nThere was an error in your regular expression:\r\n" + ex.Message);
            Console.ResetColor();
            ArrayList nAL = new ArrayList();
            return "";
        }
    }

    //using System.Text.RegularExpressions;
    public static string RegexReplace(string 正则表达式, string 替换为, string 字符串, RegexOptions 正则选项)
    {
        try
        {
            string returnstr = "";
            Regex replaceRegex = new Regex(正则表达式, 正则选项);
            returnstr = replaceRegex.Replace(字符串, 替换为);
            return returnstr;
        }
        catch (ArgumentException ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("\nThere was an error in your regular expression:\r\n" + ex.Message);
            Console.ResetColor();
            return 字符串;
        }
    }

    //注意：此代码需要using System.Text.RegularExpressions;            
    public static string[] SplitByRegex(string 句子, string 分隔依据, RegexOptions 选项)
    {
        try
        {
            String[] splitResults;
            Regex splitRegex = new Regex(分隔依据, 选项);
            splitResults = splitRegex.Split(句子);
            return splitResults;
        }
        catch (ArgumentException ex)
        {
            System.Console.Write("\nThere was an error in your regular expression:\r\n" + ex.Message);
            string[] falseResults = new string[] { };
            return falseResults;
        }
    }

    //注意：此代码需要using System.Text.RegularExpressions;            
    public static ArrayList SplitByRegexToAL(string 句子, string 分隔依据, RegexOptions 选项)
    {

        try
        {
            ArrayList rAL = new ArrayList();
            String[] splitResults;
            Regex splitRegex = new Regex(分隔依据, 选项);
            splitResults = splitRegex.Split(句子);

            foreach (string t in splitResults)
                rAL.Add(t);
            return rAL;
        }
        catch (ArgumentException ex)
        {
            ArrayList rAL = new ArrayList();
            System.Console.Write("\nThere was an error in your regular expression:\r\n" + ex.Message);
            return rAL;
        }
    }
}